import { ElNotification, ElMessageBox } from 'element-plus'

const noticTitle = '提示'

interface IDbNotic {
  success: (message?: string, noticeTitle?: string) => void
  error: (message?: string) => void
  warning: (message: string) => void
  info: (message: string) => void
  confirm: (message: string, callback: (confirm: any) => void, options?: IDbConfirmOption) => void
}
declare type DbMessageType = '' | 'success' | 'warning' | 'info' | 'error'
interface IDbConfirmOption {
  confirmButtonText: string
  cancelButtonText: string
  type: DbMessageType
}
/**
 * 提示消息
 */
export default function (): IDbNotic {
  const title = noticTitle

  const dbMsg: IDbNotic = {
    success: function (message?: string, noticeTitle?: string) {
      ElNotification({
        title: noticeTitle || title,
        message: message || '操作成功',
        type: 'success'
      })
    },
    error: function (message?: string) {
      ElNotification({
        title,
        message: message || '操作失败',
        type: 'error'
      })
    },
    warning: function (message: string) {
      ElNotification({
        title,
        message: message,
        type: 'warning'
      })
    },
    info: function (message: string) {
      ElNotification({
        title,
        message: message,
        type: 'info'
      })
    },
    confirm: function (message: string, callback: Function, options?: IDbConfirmOption) {
      ElMessageBox.confirm(message, noticTitle, {
        confirmButtonText: options?.confirmButtonText || '确定',
        cancelButtonText: options?.cancelButtonText || '取消',
        type: options?.type || 'warning'
      })
        .then(() => {
          callback(true)
        })
        .catch(() => {
          callback(false)
        })
    }
  }

  return dbMsg
}
